package dao;

import domain.Equipment;
import domain.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import util.DruidUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 简介说明: 用户数据访问层实现类
 *
 * @author: Half_Q_0115
 * @date: 2024/09/16 23:49:46
 * @version: 1.0
 */

public class UserDaoImpl {
    private QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());
  
    public int addUser(User user) {
        String sql = "INSERT INTO `user` VALUES (?,?,?,?,?,?,?);";
        user.setPassword(user.newPassword(user.getPassword()));
        Object[] param = {user.getId(), user.getName(),user.getGender(),user.getCollege_id(),user.getIdentity(),
        user.getPhone_number(),user.getPassword()};
        int result;
        try {
            result = qr.update(sql, param);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public int deleteUserById(String id)  {
        String sql = "DELETE FROM user WHERE id = ?";
        Object[] params = {id};
        int result = 0;
        try {
            result = qr.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
    
    public boolean checkUser(String username, String password){
        String sql = "select * from user where id=?";
        User user = null;
        try {
            user = qr.query(sql, new BeanHandler<User>(User.class), username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if(user == null){
            return false;
        }else{
            System.out.println(password);
            System.out.println(user.newPassword(password));
            System.out.println(user.getPassword());
            if(user.newPassword(password).equals(user.getPassword())){
                return true;
            }else{
                return false;
            }
        }
    }

    public int getIdentityById(String id) {
        String sql = "select identity from user where id=?";
        int result = 0;
        try {
            result =  qr.query(sql, new ScalarHandler<>(), id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public String getNameById(String id) {
        String sql = "select name from user where id=?";
        String name = "";
        try {
            name =  qr.query(sql, new ScalarHandler<>(), id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return name;
    }

    public boolean checkPhone(String phone_number){
        String sql = "select * from user where phone_number=?";
        User user = null;
        try {
            user = qr.query(sql, new BeanHandler<User>(User.class), phone_number);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if(user == null){
            return false;
        }else{
            return true;
        }
    }

    public String getIdByPhone(String phone_number){
        String sql = "select id from user where phone_number=?";
        String result = "";
        try {
            result = qr.query(sql, new ScalarHandler<>(), phone_number);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public int getIdentityByPhone(String phone_number) {
        String sql = "select identity from user where phone_number=?";
        int result = 0;
        try {
            result =  qr.query(sql, new ScalarHandler<>(), phone_number);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public String getNameByPhone(String phone_number) {
        String sql = "select name from user where phone_number=?";
        String result = "";
        try {
            result = qr.query(sql, new ScalarHandler<>(), phone_number);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public int getCollegeIdById(String id) {
        String sql = "select college_id from user where id=?";
        int result = 0;
        try {
            result = qr.query(sql, new ScalarHandler<>(), id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public int getCollegeIdByPhone(String phone_number) {
        String sql = "select college_id from user where phone_number=?";
        int result = 0;
        try {
            result = qr.query(sql, new ScalarHandler<>(), phone_number);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public List<User> findAllUser() {
        String sql = "select * from user";
        List<User> list = null;
        try {
            list = qr.query(sql, new BeanListHandler<User>(User.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public List<User> searchUsers(String id, String name, String gender, String college_id, String identity, String phone_number) {
        StringBuilder sql = new StringBuilder("SELECT * FROM user WHERE 1=1");
        List<Object> params = new ArrayList<>();

        if (id != null && !id.trim().isEmpty()) {
            sql.append(" AND id = ?");
            params.add(id);
        }
        if (name != null && !name.trim().isEmpty()) {
            sql.append(" AND name LIKE ?");
            params.add("%" + name + "%");
        }
        if (gender != null && !gender.trim().isEmpty()) {
            sql.append(" AND gender = ?");
            params.add(gender);
        }
        if (college_id != null && !college_id.trim().isEmpty()) {
            sql.append(" AND college_id = ?");
            params.add(Integer.parseInt(college_id));
        }
        if (identity != null && !identity.trim().isEmpty()) {
            sql.append(" AND identity = ?");
            params.add(Integer.parseInt(identity));
        }
        if (phone_number != null && !phone_number.trim().isEmpty()) {
            sql.append(" AND phone_number = ?");
            params.add(phone_number);
        }

        try {
            return qr.query(sql.toString(), new BeanListHandler<>(User.class), params.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public User findUserById(String id){
        String sql = "select * from user where id = ?";
        User user = null;
        try {
            user = qr.query(sql, new BeanHandler<>(User.class), id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    public int modifyUser(User user) {
        String sql = "UPDATE user SET name = ?, gender = ?, college_id = ?, identity = ?, phone_number=?, password=? WHERE id = ?";
        Object[] params = {user.getName(), user.getGender(), user.getCollege_id(), user.getIdentity(), user.getPhone_number(),
                user.newPassword(user.getPassword()), user.getId()};
        int result = 0;
        try {
            result = qr.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
